package com.example.Ex3Demo;
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btShow_2;
    Button btShow_3;
    Button btShow_5;
    Button btShow_4;
    TextView textView;
    ProgressBar progressBar=null;
    EditText editText1;
    EditText editText2;
    EditText editText3;
    EditText editText4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=findViewById(R.id.tv);
        btShow_3=((Button)findViewById(R.id.bt3));
        btShow_2=findViewById(R.id.bt2);
        btShow_5=findViewById(R.id.bt5);
        btShow_4=findViewById(R.id.bt4);
        progressBar=findViewById(R.id.bar);
        editText1 = (EditText) findViewById(R.id.ED1);
        editText2 = (EditText) findViewById(R.id.ED2);
        editText3 = (EditText) findViewById(R.id.ED3);
        editText4 = (EditText) findViewById(R.id.ED4);
        btShow_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    Toast.makeText(getApplicationContext(),
                            "使用匿名内部类点击了按钮",
                            Toast.LENGTH_SHORT).show();
            }
        });
        btShow_2.setOnClickListener(new BtnClickListener());
        btShow_5.setOnClickListener((new MyClick(textView)));
        ((Button)findViewById(R.id.bt1)).setOnClickListener(this);
        ((Button)findViewById(R.id.btBar)).setOnClickListener(this);
        ((Button)findViewById(R.id.btGetSystem)).setOnClickListener(this);
        btShow_4.setOnClickListener(view -> Toast.makeText(MainActivity.this,
                "lambda表达式点击按钮",Toast.LENGTH_SHORT).show());
    }
    @Override
    public void onClick(View view) {
      switch (view.getId())
      {
          case R.id.bt1:
              Toast.makeText(getApplicationContext(),
                      "使用Activity作为监听点击了按钮",
                      Toast.LENGTH_SHORT).show();
              break;
          case R.id.btBar:
          {
              TimeTickLoad timeTickLoad=new TimeTickLoad();
              timeTickLoad.execute(1000);
              break;
          }
          case R.id.btGetSystem:
          {
              Configuration config = getResources().getConfiguration();
              String screen = config.orientation == Configuration.ORIENTATION_LANDSCAPE ? "屏幕方向 == 横屏"
                      : "屏幕方向 == 竖屏";
              editText1.setText(screen);
              String mncCode = config.mnc + " ";
              editText2.setText(mncCode);
              String naviName = config.navigation == Configuration.NAVIGATION_NONAV ? "没有方向控制"
                      : config.navigation == Configuration.NAVIGATION_WHEEL ? "滚轮控制方向"
                      : config.navigation == Configuration.NAVIGATION_DPAD ? "方向键控制方向"
                      : "轨迹球控制方向";
              editText3.setText(naviName);
              String touchName = config.touchscreen == Configuration.TOUCHSCREEN_NOTOUCH ? "无触摸屏"
                      : config.touchscreen == Configuration.TOUCHSCREEN_STYLUS ? "触摸笔式的触摸屏"
                      : "接受手指的触摸屏";
              editText4.setText(touchName);

          }

      }

    }
    public void myclick(View view) {
        Toast.makeText(getApplicationContext(), "绑定到标签点击了按钮", Toast.LENGTH_SHORT).show();
    }
    class BtnClickListener implements View.OnClickListener {
         public void onClick(View view) {
                 Toast.makeText(getApplicationContext(),"使用内部类点击了按钮",Toast.LENGTH_SHORT).show();
         }
    }
    class TimeTickLoad extends AsyncTask<Integer,Integer,String>
    {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);
            textView.setText(" onPreExecute\n");
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            textView.setText(" onPostExecute");
            textView.setText(s);
        }
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            textView.setText("耗时任务执行了 :"+values[0]+"%");
            progressBar.setProgress(values[0]);
        }
        @Override
        protected String doInBackground(Integer... integers) {
            textView.setText("doInBackground");
            for(int i=0;i<=10;i++)
            {
                publishProgress(i*10);
                try {
                    Thread.sleep(integers[0]);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return "进度执行完成";
        }
    }

}